package course.view;

import java.awt.*;
<<<<<<< HEAD
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import course.controller.CourseControllerListener;
import course.db.DbConnection;
import course.po.Course;
import course.po.Student;
import course.service.impl.CourseServiceImpl;

public class StudentFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private static CourseControllerListener controllerListener;
	public static JScrollPane tablePanel;
	public CourseServiceImpl courseService;
	private String accountName;
	public static String[] strArray ;
	public static ArrayList<String> list;
	public static Map map;
	public static String[] num;
	public static JPanel p1;
	public static JPanel p2;
	private JPanel p3;
	private JPanel p4;
	private JPanel p5;
	private JPanel p6;
	private JPanel p7;
	private JPanel p8;
	private JPanel p9;
	private JPanel p10;
	private JPanel p11;
	private JPanel p12;
	private JPanel p13;
	private JPanel p14;
	private JPanel p15;
	private JPanel p16;
	private JPanel p17;
	private JPanel p18;
	private JPanel p19;
	private JPanel p20;
	private JPanel p21;
	private JPanel p22;
	private JPanel p23;
	private JPanel p24;
	public JPanel p25;
	public static JTextField t1;
	public static JScrollPane courseTablePanel;
	public static JTextField t2;
	public static List<Course> courseList;
	public static boolean state = false;
	public static int isbn = -1;// 选中的表格行索引
	public static File file;
	public StudentFrame() {
		file = new File("course.txt");
		StudentFrame.controllerListener = new CourseControllerListener();
		StudentFrame.controllerListener.setStudentFrame(this);
		JSplitPane sp1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		JSplitPane sp3 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		JSplitPane sp4 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		p4 = new JPanel();
		p5 = new JPanel();
		p6 = new JPanel();
		p7 = new JPanel();
		p8 = new JPanel();
		p9 = new JPanel();
		p10 = new JPanel();
		p11 = new JPanel();
		p12 = new JPanel();
		p13 = new JPanel();
		courseList = new ArrayList<Course>();
		courseService = new CourseServiceImpl();
		accountName = courseService.getStudent(CourseControllerListener.nameInput).getStuName();
		JLabel s1 = new JLabel("欢迎您！");
		JLabel s2 = new JLabel(accountName + "同学");
		JButton b1 = new JButton("个人信息");
		b1.addActionListener(controllerListener);
		b1.setActionCommand("studentData");
		JButton b2 = new JButton("选课");
		b2.addActionListener(controllerListener);
		b2.setActionCommand("selectCourse");
		JButton b3 = new JButton("我的课程");
		b3.addActionListener(controllerListener);
		b3.setActionCommand("myCourse");
		JButton b4 = new JButton("成绩查询");
		b4.addActionListener(controllerListener);
		b4.setActionCommand("searchGrade");
		JButton b5 = new JButton("退课");
		b5.addActionListener(controllerListener);
		b5.setActionCommand("deleteCourse");
		JButton b6 = new JButton("退出系统");
		b6.addActionListener(controllerListener);
		b6.setActionCommand("gobackS");
		sp1.setDividerLocation(100);
		sp1.setDividerSize(8);
		sp2.setLeftComponent(sp4);
		sp2.setRightComponent(p2);// 右部显示界面
		sp2.setDividerLocation(250);
		sp2.setDividerSize(8);
		sp2.setOneTouchExpandable(true);
		sp2.setContinuousLayout(true);
		sp1.setRightComponent(sp2);
		this.add(sp1);
		sp3.setLeftComponent(p3);
		sp3.setRightComponent(p4);
		sp3.setDividerLocation(250);
		sp3.setDividerSize(8);
		sp3.setOneTouchExpandable(true);
		sp3.setContinuousLayout(true);
		sp4.add(sp3);
		p3.setLayout(new GridLayout(2, 1));
		p3.add(p5);
		p3.add(p6);
		s1.setFont(new Font("Dialog", 1, 25));
		s2.setFont(new Font("Dialog", 1, 25));
		p6.add(s1);
		p6.add(s2);
		p4.setLayout(new GridLayout(7, 1));
		p4.add(p7);
		p4.add(p8);
		p4.add(p9);
		p4.add(p10);
		p4.add(p11);
		p4.add(p12);
		p4.add(p13);
		b1.setPreferredSize(new Dimension(150, 30));
		b2.setPreferredSize(new Dimension(150, 30));
		b3.setPreferredSize(new Dimension(150, 30));
		b4.setPreferredSize(new Dimension(150, 30));
		b5.setPreferredSize(new Dimension(150, 30));
		b6.setPreferredSize(new Dimension(150, 30));
		p8.add(b1);
		p9.add(b2);
		p10.add(b3);
		p11.add(b4);
		p12.add(b5);
		p13.add(b6);
		this.setTitle("选课管理系统——学生");
		this.setSize(1000, 800);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.validate();
		this.setVisible(true);
	}

	// 学生个人信息界面
		public void drawStudent() {
			p13 = new JPanel();
			p14 = new JPanel();
			p15 = new JPanel();
			p16 = new JPanel();
			p17 = new JPanel();
			p18 = new JPanel();
			p19 = new JPanel();
			p2.removeAll();
			p2.repaint();
			Student stu = courseService.getStudent(CourseControllerListener.nameInput);
			p2.setLayout(new GridLayout(8, 1));
			p2.add(p13);
			p2.add(p14);
			p2.add(p15);
			p2.add(p16);
			p2.add(p17);
			p2.add(p18);
			p2.add(p19);
			JLabel person1 = new JLabel("学号：" + stu.getStuNo());
			JLabel person2 = new JLabel("姓名：" + stu.getStuName());
			JLabel person3 = new JLabel("性别：" + stu.getStuSex());
			JLabel person4 = new JLabel("年龄：" + stu.getStuAge());
			JLabel person5 = new JLabel("班级：" + stu.getStuClassNo());
			JLabel person6 = new JLabel("专业：" + stu.getStuMajor());
			person1.setFont(new Font("Dialog", 1, 25));
			person2.setFont(new Font("Dialog", 1, 25));
			person3.setFont(new Font("Dialog", 1, 25));
			person4.setFont(new Font("Dialog", 1, 25));
			person5.setFont(new Font("Dialog", 1, 25));
			person6.setFont(new Font("Dialog", 1, 25));
			p14.add(person1);
			p15.add(person2);
			p16.add(person3);
			p17.add(person4);
			p18.add(person5);
			p19.add(person6);
			p2.revalidate();

		}

//学生我的课程界面
	public void myCourse(List<Course> courseList) {
		p2.removeAll();
		p2.repaint();
		p2.setLayout(new GridLayout(2, 1));
		if (courseList != null) {
			courseTablePanel = CourseJPanel.getCourseTablePanel(courseList);
			p2.add(courseTablePanel);
		}
		JButton b6 = new JButton("查询学期总学分");
		b6.addActionListener(controllerListener);
		b6.setActionCommand("findLCourse");
		JButton b7 = new JButton("查看总学分");
		b7.addActionListener(controllerListener);
		b7.setActionCommand("FindACourse");
		JLabel s2 = new JLabel("输入开课学年和学期：");
		p20 = new JPanel();
		p21 = new JPanel();
		p22 = new JPanel();
		p23 = new JPanel();
		p24 = new JPanel();
		p20.setLayout(new GridLayout(4, 1));
		p2.add(p20);
		p20.add(p21);
		p20.add(p22);
		p20.add(p23);
		p20.add(p24);
		s2.setFont(new Font("Dialog", 1, 25));
		t1 = new JTextField(12);
		p21.add(s2);
		p22.add(t1);
		p22.add(b6);
		p24.add(b7);
		p2.revalidate();
	}
	//学生我的课程成绩界面
		public void myCourseGrade(List<Course> courseList) {
			p2.removeAll();
			p2.repaint();
			p2.setLayout(new GridLayout(2, 1));
			if (courseList != null) {
				courseTablePanel = CourseJPanel.getCourseTablePanel(courseList);
				p2.add(courseTablePanel);
			}
			JButton b6 = new JButton("查询成绩");
			b6.addActionListener(controllerListener);
			b6.setActionCommand("searchLGrade");
			JButton b7 = new JButton("查看平均分");
			b7.addActionListener(controllerListener);
			b7.setActionCommand("serachAGarde");
			JLabel s2 = new JLabel("输入课程名：");
			p20 = new JPanel();
			p21 = new JPanel();
			p22 = new JPanel();
			p23 = new JPanel();
			p24 = new JPanel();
			p20.setLayout(new GridLayout(4, 1));
			p2.add(p20);
			p20.add(p21);
			p20.add(p22);
			p20.add(p23);
			p20.add(p24);
			s2.setFont(new Font("Dialog", 1, 25));
			t1 = new JTextField(12);
			p21.add(s2);
			p22.add(t1);
			p22.add(b6);
			p24.add(b7);
			p2.revalidate();
		}



	// 学生选课界面
	public void selectCourse() {
		p2.removeAll();
		p2.repaint();
		p2.setLayout(new GridLayout(2, 1));
		tablePanel = CourseJPanel.getCourseTablePanel(controllerListener.getCourseService().findAllCourse());
		p2.add(tablePanel);
		 
		JButton b6 = new JButton("查询");
		b6.addActionListener(controllerListener);
		b6.setActionCommand("findCourse");
		JButton b7 = new JButton("确认选课");
		b7.addActionListener(controllerListener);
		b7.setActionCommand("confirmCourse");
		JLabel s2 = new JLabel("输入开课学年和学期：");
		JLabel s3 = new JLabel("输入需要选修课程的编号：");
		p20 = new JPanel();
		p21 = new JPanel();
		p22 = new JPanel();
		p23 = new JPanel();
		p24 = new JPanel();
		p20.setLayout(new GridLayout(4, 1));
		p2.add(p20);
		p20.add(p21);
		p20.add(p22);
		p20.add(p23);
		p20.add(p24);
		s2.setFont(new Font("Dialog", 1, 25));
		s3.setFont(new Font("Dialog", 1, 25));
		t1 = new JTextField(12);
		t2 = new JTextField(12);
		p21.add(s2);
		p22.add(t1);
		p22.add(b6);
		p23.add(s3);
		p24.add(t2);
		p24.add(b7);
		p2.revalidate();
	}
	// 学生查询课程得到界面
	public void sCourse(String term) {
		
		tablePanel = CourseJPanel.getCourseTablePanel(controllerListener.getCourseService().getCourse(term));
		p2.removeAll();
		p2.repaint();
		p2.setLayout(new GridLayout(2, 1));
		p2.add(tablePanel);
		JButton b6 = new JButton("查询");
		b6.addActionListener(controllerListener);
		b6.setActionCommand("findCourse");
		JButton b7 = new JButton("确认选课");
		b7.addActionListener(controllerListener);
		b7.setActionCommand("confirmCourse");
		JLabel s2 = new JLabel("输入开课学年和学期：");
		JLabel s3 = new JLabel("输入需要选修课程的编号：");
		p20 = new JPanel();
		p21 = new JPanel();
		p22 = new JPanel();
		p23 = new JPanel();
		p24 = new JPanel();
		p20.setLayout(new GridLayout(4, 1));
		p2.add(p20);
		p20.add(p21);
		p20.add(p22);
		p20.add(p23);
		p20.add(p24);
		s2.setFont(new Font("Dialog", 1, 25));
		s3.setFont(new Font("Dialog", 1, 25));
		t1 = new JTextField(12);
		t2 = new JTextField(12);
		p21.add(s2);
		p22.add(t1);
		p22.add(b6);
		p23.add(s3);
		p24.add(t2);
		p24.add(b7);
		p2.revalidate();
	}
	public static void refreshNum(List<Course> courseList) {
		System.out.println("测试——当前课程数量为：" + courseList.size());
		p1.remove(tablePanel);
		p1.add(CourseJPanel.getCourseTablePanel(courseList),
				BorderLayout.CENTER);
		p1.validate();
		JOptionPane.showMessageDialog(p1, "重新加载数据成功！");
	}

	public CourseControllerListener getControllerListener() {
		return controllerListener;
	}
//	//使用Set
//	public static boolean useSet(String[] arr,String targetValue){
//	    Set<String> set=new HashSet<String>(Arrays.asList(arr));
//	    return set.contains(targetValue);
//	}
	public static void readOut() {
		StringBuilder result = new StringBuilder();
		 //构造一个BufferedReader类来读取文件
           BufferedReader br = null;
			try {
				br = new BufferedReader(new FileReader(StudentFrame.file));
			} catch (FileNotFoundException e3) {
				e3.printStackTrace();
			}       
           String s = null;
           try {
				while((s = br.readLine())!=null)
				{
					list= new ArrayList<>();
					StudentFrame.list.add(s);
				 //使用readLine方法，一次读一行	
					for(int i=0;i<StudentFrame.list.size();i++) {
						
				
				 }
				}
			} catch (IOException e2) {
				e2.printStackTrace();
			}
=======
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import course.controller.CourseControllerListener;
import course.db.DbConnection;
import course.po.Course;
import course.po.Student;
import course.service.impl.CourseServiceImpl;

public class StudentFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private static CourseControllerListener controllerListener;
	public static JScrollPane tablePanel;
	public CourseServiceImpl courseService;
	private String accountName = courseService.getStudent(CourseControllerListener.nameInput).getStuName();
	public static JPanel p1;
	public static JPanel p2;
	private JPanel p3;
	private JPanel p4;
	private JPanel p5;
	private JPanel p6;
	private JPanel p7;
	private JPanel p8;
	private JPanel p9;
	private JPanel p10;
	private JPanel p11;
	private JPanel p12;
	private JPanel p13;
	private JPanel p14;
	private JPanel p15;
	private JPanel p16;
	private JPanel p17;
	private JPanel p18;
	private JPanel p19;
	private JPanel p20;
	private JPanel p21;
	private JPanel p22;
	private JPanel p23;
	private JPanel p24;
	public JPanel p25;
	public static JTextField t1;
	public static JScrollPane courseTablePanel;
	public static JTextField t2;
	public static List<Course> courseList;
	public static int isbn = -1;// 选中的表格行索引

	public StudentFrame() {
		StudentFrame.controllerListener = new CourseControllerListener();
		StudentFrame.controllerListener.setStudentFrame(this);
		JSplitPane sp1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		JSplitPane sp3 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		JSplitPane sp4 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		p4 = new JPanel();
		p5 = new JPanel();
		p6 = new JPanel();
		p7 = new JPanel();
		p8 = new JPanel();
		p9 = new JPanel();
		p10 = new JPanel();
		p11 = new JPanel();
		p12 = new JPanel();
		courseList = new ArrayList<Course>();
		JLabel s1 = new JLabel("欢迎您！");
		JLabel s2 = new JLabel(accountName + "同学");
		JButton b1 = new JButton("个人信息");
		b1.addActionListener(controllerListener);
		b1.setActionCommand("studentData");
		JButton b2 = new JButton("选课");
		b2.addActionListener(controllerListener);
		b2.setActionCommand("selectCourse");
		JButton b3 = new JButton("我的课程");
		b3.addActionListener(controllerListener);
		b3.setActionCommand("myCourse");
		JButton b4 = new JButton("成绩查询");
		b4.addActionListener(controllerListener);
		b4.setActionCommand("searchGrade");
		JButton b5 = new JButton("退课");
		b5.addActionListener(controllerListener);
		b5.setActionCommand("deleteCourse");
		sp1.setDividerLocation(100);
		sp1.setDividerSize(8);
		sp2.setLeftComponent(sp4);
		sp2.setRightComponent(p2);// 右部显示界面
		sp2.setDividerLocation(250);
		sp2.setDividerSize(8);
		sp2.setOneTouchExpandable(true);
		sp2.setContinuousLayout(true);
		sp1.setRightComponent(sp2);
		this.add(sp1);
		sp3.setLeftComponent(p3);
		sp3.setRightComponent(p4);
		sp3.setDividerLocation(250);
		sp3.setDividerSize(8);
		sp3.setOneTouchExpandable(true);
		sp3.setContinuousLayout(true);
		sp4.add(sp3);
		p3.setLayout(new GridLayout(2, 1));
		p3.add(p5);
		p3.add(p6);
		s1.setFont(new Font("Dialog", 1, 25));
		s2.setFont(new Font("Dialog", 1, 25));
		p6.add(s1);
		p6.add(s2);
		p4.setLayout(new GridLayout(6, 1));
		p4.add(p7);
		p4.add(p8);
		p4.add(p9);
		p4.add(p10);
		p4.add(p11);
		p4.add(p12);
		b1.setPreferredSize(new Dimension(150, 30));
		b2.setPreferredSize(new Dimension(150, 30));
		b3.setPreferredSize(new Dimension(150, 30));
		b4.setPreferredSize(new Dimension(150, 30));
		b5.setPreferredSize(new Dimension(150, 30));
		p8.add(b1);
		p9.add(b2);
		p10.add(b3);
		p11.add(b4);
		p12.add(b5);
		this.setTitle("选课管理系统——学生");
		this.setSize(1000, 800);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.validate();
		this.setVisible(true);
	}

	// 学生个人信息界面
	public void drawStudent() {
		p13 = new JPanel();
		p14 = new JPanel();
		p15 = new JPanel();
		p16 = new JPanel();
		p17 = new JPanel();
		p18 = new JPanel();
		p19 = new JPanel();
		p2.removeAll();
		p2.repaint();
		Student stu = courseService.getStudent(CourseControllerListener.nameInput);
		p2.setLayout(new GridLayout(8, 1));
		p2.add(p13);
		p2.add(p14);
		p2.add(p15);
		p2.add(p16);
		p2.add(p17);
		p2.add(p18);
		p2.add(p19);
		JLabel person1 = new JLabel("学号：" + stu.getStuNo());
		JLabel person2 = new JLabel("姓名：" + stu.getStuName());
		JLabel person3 = new JLabel("性别：" + stu.getStuSex());
		JLabel person4 = new JLabel("年龄：" + stu.getStuAge());
		JLabel person5 = new JLabel("班级：" + stu.getStuClassNo());
		JLabel person6 = new JLabel("专业：" + stu.getStuMajor());
		person1.setFont(new Font("Dialog", 1, 25));
		person2.setFont(new Font("Dialog", 1, 25));
		person3.setFont(new Font("Dialog", 1, 25));
		person4.setFont(new Font("Dialog", 1, 25));
		person5.setFont(new Font("Dialog", 1, 25));
		person6.setFont(new Font("Dialog", 1, 25));
		p14.add(person1);
		p15.add(person2);
		p16.add(person3);
		p17.add(person4);
		p18.add(person5);
		p19.add(person6);
		p2.revalidate();

	}

//学生我的课程界面
	public void myCourse() {
		p2.removeAll();
		p2.repaint();
		p2.setLayout(new GridLayout(2, 1));
		if (courseList != null) {
			courseTablePanel = CourseJPanel.getCourseTablePanel(courseList);
			p2.add(courseTablePanel);
		}

	}

//学生选课功能
	public void addMyCourse() throws SQLException {
		hadSelectNum();
		courseList.add(controllerListener.getCourseService().selectCourse(Integer.valueOf(t2.getText())));

	}

//学生已选课程状态
	public void hadSelectNum() {
		Connection conn = DbConnection.getConnection();
		String sql = "update course set course_select_number= course_select_number +1  WHERE course_id=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, Integer.valueOf(t2.getText()));
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(null, ps);
		}
	}

	// 学生选课界面
	public void selectCourse() {
		p2.removeAll();
		p2.repaint();
		p2.setLayout(new GridLayout(2, 1));
		p2.add(p1);
		p1.setLayout(new BorderLayout());
		tablePanel = CourseJPanel.getCourseTablePanel(controllerListener.getCourseService().findAllCourse());
		p1.add(tablePanel);
		JButton b6 = new JButton("查询");
		b6.addActionListener(controllerListener);
		b6.setActionCommand("findCourse");
		JButton b7 = new JButton("确认选课");
		b7.addActionListener(controllerListener);
		b7.setActionCommand("confirmCourse");
		JLabel s2 = new JLabel("输入开课学年和学期：");
		JLabel s3 = new JLabel("输入需要选修课程的编号：");
		p20 = new JPanel();
		p21 = new JPanel();
		p22 = new JPanel();
		p23 = new JPanel();
		p24 = new JPanel();
		p20.setLayout(new GridLayout(4, 1));
		p2.add(p20);
		p20.add(p21);
		p20.add(p22);
		p20.add(p23);
		p20.add(p24);
		s2.setFont(new Font("Dialog", 1, 25));
		s3.setFont(new Font("Dialog", 1, 25));
		t1 = new JTextField(12);
		t2 = new JTextField(12);
		p21.add(s2);
		p22.add(t1);
		p22.add(b6);
		p23.add(s3);
		p24.add(t2);
		p24.add(b7);
		p2.revalidate();
	}

	public static void refreshNum(List<Course> courseList) {
		System.out.println("测试——当前课程数量为：" + courseList.size());
		p1.remove(tablePanel);
		p1.add(CourseJPanel.getCourseTablePanel(controllerListener.getCourseService().findAllCourse()),
				BorderLayout.CENTER);
		p1.validate();
		JOptionPane.showMessageDialog(p1, "重新加载数据成功！");
	}

	public CourseControllerListener getControllerListener() {
		return controllerListener;
>>>>>>> refs/remotes/origin/master
	}
}
